package vip.liux.contracts.repositories.point;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.ListPagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import vip.liux.contracts.models.point.PointsTransaction;
import vip.liux.contracts.shared.enums.point.TransactionStatus;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Optional;

public interface IPointsTransactionRepository extends ListPagingAndSortingRepository<PointsTransaction, Long>, ListCrudRepository<PointsTransaction, Long> {
    BigDecimal sumTodayPointsByUserAndRule(
            @Param("userId") Long userId,
            @Param("start") LocalDateTime start,
            @Param("end") LocalDateTime end);

    Optional<PointsTransaction> findByRelatedNoAndRuleCode(String relatedNo, String ruleCode);

    Page<PointsTransaction> findByUserId(Long userId, String transactionNo, String relatedNo, String ruleCode, LocalDate transactionDateStart, LocalDate transactionDateEnd, Pageable page);

    Optional<PointsTransaction> findByTransactionNo(@NotNull String transactionNo);

    Collection<PointsTransaction> findByRelatedNo(@NotBlank(message = "原交易流水号不能为空") String relatedNo);

    int updateStatusByIdAndStatus(TransactionStatus status, Long id, TransactionStatus newStatus);
}